/**
 * 
 */
package com.vurenium.ras.patient;


//import javax.faces.context.ExternalContext;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.ResultDataModel;
//import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.jstl.sql.Result;

import com.vurenium.ras.security.Login;
import com.vurenium.ras.util.DBHelper;

/**
 * @author svaish
 *
 */
public class Patient {
	private String id;
	private String firstName;
	private String lastName;
	private String address;
	private String homePh;
	private String workPh;
	private String cellPh;
	private String healthNum;
	//TODO: perhaps hide the relationship for UI
	private String authId;
	private String selectedId;
	private ResultDataModel dataModel;
	
	public ResultDataModel getDataModel() {
		return dataModel;
	}
	public void setDataModel(ResultDataModel dataModel) {
		this.dataModel = dataModel;
	}
	public String getSelectedId() {
		return selectedId;
	}
	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getAuthId() {
		return authId;
	}
	public void setAuthId(String authId) {
		this.authId = authId;
	}
	public String getCellPh() {
		return cellPh;
	}
	public void setCellPh(String cellPh) {
		this.cellPh = cellPh;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getHealthNum() {
		return healthNum;
	}
	public void setHealthNum(String healthNum) {
		this.healthNum = healthNum;
	}
	public String getHomePh() {
		return homePh;
	}
	public void setHomePh(String homePh) {
		this.homePh = homePh;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getWorkPh() {
		return workPh;
	}
	public void setWorkPh(String workPh) {
		this.workPh = workPh;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	/** 
	 * called by action button
	 * @return
	 */
	public String find() {
		String returnValue="notFound";
		System.out.println("here1");
		try {
			ResultDataModel r = DBHelper.select("select * from patient where pat_fname like '%"+getFirstName()+"%' and pat_lname like '%"+getLastName()+"%'" );
			System.out.println("*** DB result***"+r.getRowCount());
			
			if (r.getRowCount()>0){
				// add the result to session
				//ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
				//HttpServletRequest request =  (HttpServletRequest)context.getRequest();

				Login a = (Login)FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(), "loginBean");
				System.out.println("**hello:"+a.getLoginName());
				if (r.getRowCount()==1){				
					this.setSelectedId((((Result)r.getWrappedData()).getRows()[0].get("pat_id")).toString());
					System.out.println("**id:"+this.getSelectedId());
					returnValue="foundOne"; 
				}else {
					this.setDataModel(r);
					returnValue="foundMany";
				}
			}
		}catch (Exception ex){
			//TODO: exception management
			ex.printStackTrace();
		}
		return returnValue;
	}
	public String select() {
		String returnValue="foundOne";
		System.out.println("here2");
		try {
			System.out.println("*** try result 1***"+this.getDataModel().getRowData());
			this.setSelectedId((((Map)this.getDataModel().getRowData()).get("pat_id")).toString());
		}catch (Exception ex){
			//TODO: exception management
			ex.printStackTrace();
		}
		return returnValue;
	}

}
